package ro.dandanciu.webgallery.ui.galleries;

import java.util.List;

import org.apache.log4j.Logger;

import com.opensymphony.xwork2.Preparable;

import ro.dandanciu.webgallery.domainmodel.Gallery;
import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.ui.general.WebGalleryAction;

public class GalleryManagementAction extends WebGalleryAction implements Preparable{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GalleryManagementAction.class);

	/**
	 * The serial version id.
	 */
	private static final long serialVersionUID = -1546392159764218794L;

	private static final String ADD_PAGE = "AP";

	private static final String ADD = "A";

	private static final String LIST = "L";

	private static final String LIST_FORWARD = "listByOwner";
	
	private static final String MAKE_PRIVATE = "MPr";

	private static final String MAKE_PUBLIC = "MPu";

	/** The operation identifier. */
	private String mode;

	/** The name of the gallery. */
	private String name;

	/** The description of the gallery. */
	private String description;
	
	private boolean publicAccess;
	
	private long galleryId = -1;
	
	private boolean showUploadPictures = false;

	private List<Gallery> galleries;
	
	private Gallery gallery;
	
	private String query = "";
	
	
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		String forward = null;
		if (ADD_PAGE.equals(mode)) {
			forward = "add_page";
		} else if (ADD.equals(mode)) {
			executeAdd();
			forward = setMessage("gallery.add.success");
		} else if (LIST.equals(mode)) {
			executeList();
			forward = LIST_FORWARD;
		} else if (MAKE_PRIVATE.equals(mode)) {
			executeChangeAccess(false);
			forward = LIST_FORWARD;
		} else if (MAKE_PUBLIC.equals(mode)) {
			executeChangeAccess(true);
			forward = LIST_FORWARD;		
		} else {
			forward = setMessage("failure");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("execute() - end");
		}
		return forward;
	}
	
	private void executeChangeAccess(boolean publicAccess) {
		gallery = galleryServices.getGalleryById(galleryId);
		gallery.setPublicAccess(publicAccess);
		galleryServices.update(gallery);
		executeList();
	}

	private void executeList() {
		showUploadPictures = (galleryId > 0);
		gallery = galleryServices.getGalleryById(galleryId);
		galleries = galleryServices.getGalleriesByOwner(
				getSessionUser(), galleryId, query);
	}

	private void executeAdd() {
		
		User owner = getSessionUser();
		Gallery gallery = new Gallery();
		gallery.setName(name);
		if (galleryId > 0) {
			Gallery parent = galleryServices.getGalleryById(galleryId);
			gallery.setParent(parent);
		} else {
			gallery.setParent(null);
		}
		gallery.setOwner(owner);
		gallery.setDescription(description);
		gallery.setPublicAccess(publicAccess);
		galleryServices.save(gallery);
	}

	
	public void prepare() throws Exception {
		// TODO[prepare] add code
		
	}


	public String getMode() {
		return mode;
	}


	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	public List<Gallery> getGalleries() {
		return galleries;
	}

	public long getGalleryId() {
		return galleryId;
	}

	public void setGalleryId(long galleryId) {
		this.galleryId = galleryId;
	}

	public boolean isShowUploadPictures() {
		return showUploadPictures;
	}

	public void setShowUploadPictures(boolean showUploadPictures) {
		this.showUploadPictures = showUploadPictures;
	}

	public boolean isPublicAccess() {
		return publicAccess;
	}

	public void setPublicAccess(boolean publicAccess) {
		this.publicAccess = publicAccess;
	}

	public Gallery getGallery() {
		return gallery;
	}

	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}
}
